<template>
  <el-breadcrumb separator="/" class="mb-2 mt-1" style="vertical-align: bottom">
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <Goods /> </el-icon
        ><span class="pl">电商管理</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>訂單管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card" shadow="hover">
    <div class="tab_nav">
      <el-link class="history" :underline="false">历史订单</el-link>
      <el-tabs
        v-model="params.status_nav_id"
        class="demo-tabs"
        @tab-click="handleClick"
      >
        <el-tab-pane
          v-for="(item, index) in filter.status_nav"
          :label="item.name"
          :name="item.id"
        ></el-tab-pane>
      </el-tabs>
    </div>

    <el-space wrap :size="10">
      <el-input
        v-model="params.order_sn"
        style="width: 150px"
        placeholder="訂單編號"
      />
      <el-input
        v-model="params.fb_user_id"
        style="width: 150px"
        placeholder="用戶ID"
      />
      <el-input
        v-model="params.consignee"
        style="width: 150px"
        placeholder="收件人姓名"
      />
      <el-input
        v-model="params.consignee_tel"
        style="width: 150px"
        placeholder="收件人電話"
      />
      <el-input
        v-model="params.buyer"
        style="width: 150px"
        placeholder="購買人"
      />
      <el-input
        v-model="params.first_letter"
        style="width: 150px"
        placeholder="用戶名首字母"
      />
      <el-select
        v-model="params.status_id"
        placeholder="狀態"
        style="width: 150px"
        clearable
      >
        <el-option
          v-for="item in filter.status"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="params.order_source_id"
        placeholder="訂單來源"
        style="width: 150px"
        clearable
      >
        <el-option
          v-for="item in filter.source"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="params.more_cart_id"
        placeholder="多重購物車"
        style="width: 150px"
      >
        <el-option
          v-for="item in filter.morcart"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="params.pay_id"
        placeholder="付款方式"
        style="width: 150px"
        clearable
      >
        <el-option
          v-for="item in filter.pay"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="params.print_id"
        placeholder="打印狀態(出貨單)"
        style="width: 150px"
        clearable
      >
        <el-option
          v-for="item in filter.print"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="params.date_type_id" style="width: 150px">
        <el-option
          v-for="item in filter.datetype"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-space>
    <div class="flex justify-between flex-wrap mb-2 mt">
      <el-space wrap :size="10">
        <el-date-picker
          v-model="myDate"
          type="daterange"
          :clearable="false"
          range-separator="To"
          start-placeholder="開始日期"
          end-placeholder="結束日期"
          :disabled-date="disabledDateFun"
        />
        <el-checkbox v-model="params.is_note_image" label="圖片備註" />
        <el-button :icon="Search" plain @click="search">查詢</el-button>
      </el-space>
      <el-space wrap :size="10" class="mt-1">
        <el-button type="primary" plain>下載</el-button>
        <el-button type="primary" plain>物流訂單下載</el-button>
        <el-button type="primary" plain>廠商報表</el-button>
      </el-space>
    </div>
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="訂單編號" align="center" width="150">
        <template #default="scope">
          <div class="flex flex-column align-center">
            <div class="mt">{{ scope.row.order_sn }}</div>
            <el-image
              style="width: 100px"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :z-index="999"
              preview-teleported
              hide-on-click-modal
              :preview-src-list="[scope.row.barcode]"
              :src="scope.row.barcode"
              fit="contain"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="order_price" label="訂單金額" />
      <el-table-column prop="pay_way" label="付款方式" />
      <el-table-column prop="ship_way" label="運送方式" />
      <el-table-column prop="consignee" label="收件人" />
      <el-table-column prop="buyer" label="購買人" />
      <el-table-column prop="order_time" label="下單時間" width="150" />
      <el-table-column prop="status_txt" label="狀態" />
      <el-table-column prop="order_source" label="訂單來源" />
      <el-table-column prop="morecart" label="多重購物車" width="150" />
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="scope">
          <el-button
            link
            size="small"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button
          >
          <el-button
            link
            size="small"
            type="danger"
            @click="handleEdit(scope.$index, scope.row)"
            >私訊對話</el-button
          >
          <el-button
            link
            size="small"
            type="info"
            @click="handleDelete(scope.$index, scope.row)"
            >用戶分組</el-button
          >
          <div class="font-sm">列印拣货单：{{ scope.row.pick_num }}</div>
          <div class="font-sm">列印出货单：{{ scope.row.delivery_num }}</div>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[50, 100, 150]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, Goods } from "@element-plus/icons-vue";
import {
  $status_nav,
  $status,
  $shipping,
  $source,
  $morcart,
  $pay,
  $print,
  $datetype,
  $getOrder,
} from "@api/live";
import Uncart from "./cart/Uncart.vue";
import Allcart from "./cart/Allcart.vue";
import { dayjs } from "element-plus";
const myDate = ref([]);
let params = ref({
  status_nav_id: "",
  order_sn: "",
  fb_user_id: "",
  consignee: "",
  consignee_tel: "",
  buyer: "",
  ship_id: "",
  first_letter: "",
  status_id: "",
  order_source_id: "",
  more_cart_id: "",
  pay_id: "",
  print_id: "",
  date_type_id: "",
  start_time: "",
  end_time: "",
  is_note_image: "",
  current_page: 1,
  page_size: 50,
});
const filter = ref({
  status_nav: [],
  status: [],
  shipping: [],
  source: [],
  morcart: [],
  pay: [],
  print: [],
  datetype: [],
});
const loading = ref(false);
const total = ref(0);
const tableData = ref([]);
onMounted(() => {
  getFilter();
});
const getFilter = async () => {
  loading.value = true;
  const status_nav_res = await $status_nav();
  const status_res = await $status();
  const shipping_res = await $shipping();
  const source_res = await $source();
  const morcart_res = await $morcart();
  const pay_res = await $pay();
  const print_res = await $print();
  const datetype_res = await $datetype();
  filter.value.status_nav = status_nav_res.data.list;
  filter.value.status = status_res.data.list;
  filter.value.shippingv = shipping_res.data.list;
  filter.value.source = source_res.data.list;
  filter.value.morcart = morcart_res.data.list;
  filter.value.pay = pay_res.data.list;
  filter.value.print = print_res.data.list;
  filter.value.datetype = datetype_res.data.list;
  //初始化默认值
  params.value.status_nav_id = status_nav_res.data.list[0].id;
  params.value.more_cart_id = morcart_res.data.list[0].id;
  params.value.date_type_id = datetype_res.data.list[0].id;
  loading.value = false;
  getData();
};
const getData = async () => {
  loading.value = true;
  if (myDate.value.length != 0) {
    params.value.start_time = dayjs(myDate.value[0]).format("YYYY-MM-DD");
    params.value.end_time = dayjs(myDate.value[1]).format("YYYY-MM-DD");
  }
  let res = await $getOrder(params);
  console.log(res);
  loading.value = false;
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(params);
  getData();
};
const disabledDateFun = (date) => {
  let now = dayjs().subtract(1, "day");
  if (date < now) return false;
  return true;
};
</script>

<style lang="scss" scoped>
.tab_nav {
  position: relative;
  .history {
    position: absolute;
    right: 0px;
    top: 9px;
    z-index:99;
  }
}
</style>
